package com.geo.business.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.geo.entity.Business;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 企业信息Mapper接口
 * 提供企业数据访问功能
 * 
 * @author GEO Team
 * @version 1.0.0
 */
@Mapper
public interface BusinessMapper extends BaseMapper<Business> {
    
    /**
     * 根据企业名称查询企业
     * @param businessName 企业名称
     * @return 企业信息
     */
    @Select("SELECT * FROM biz_business WHERE business_name = #{businessName} AND deleted = 0")
    Business findByBusinessName(@Param("businessName") String businessName);
    
    /**
     * 根据企业编码查询企业
     * @param businessCode 企业编码
     * @return 企业信息
     */
    @Select("SELECT * FROM biz_business WHERE business_code = #{businessCode} AND deleted = 0")
    Business findByBusinessCode(@Param("businessCode") String businessCode);
    
    /**
     * 根据统一社会信用代码查询企业
     * @param creditCode 统一社会信用代码
     * @return 企业信息
     */
    @Select("SELECT * FROM biz_business WHERE credit_code = #{creditCode} AND deleted = 0")
    Business findByCreditCode(@Param("creditCode") String creditCode);
    
    /**
     * 根据联系邮箱查询企业
     * @param email 联系邮箱
     * @return 企业信息
     */
    @Select("SELECT * FROM biz_business WHERE contact_email = #{email} AND deleted = 0")
    Business findByContactEmail(@Param("email") String email);
    
    /**
     * 根据认证状态查询企业列表
     * @param verificationStatus 认证状态
     * @return 企业列表
     */
    @Select("SELECT * FROM biz_business WHERE verification_status = #{verificationStatus} AND deleted = 0 ORDER BY create_time DESC")
    List<Business> findByVerificationStatus(@Param("verificationStatus") String verificationStatus);
    
    /**
     * 根据账户状态查询企业列表
     * @param accountStatus 账户状态
     * @return 企业列表
     */
    @Select("SELECT * FROM biz_business WHERE account_status = #{accountStatus} AND deleted = 0 ORDER BY create_time DESC")
    List<Business> findByAccountStatus(@Param("accountStatus") String accountStatus);
    
    /**
     * 根据订阅计划查询企业列表
     * @param subscriptionPlan 订阅计划
     * @return 企业列表
     */
    @Select("SELECT * FROM biz_business WHERE subscription_plan = #{subscriptionPlan} AND deleted = 0")
    List<Business> findBySubscriptionPlan(@Param("subscriptionPlan") String subscriptionPlan);
    
    /**
     * 根据行业查询企业列表
     * @param industry 行业
     * @return 企业列表
     */
    @Select("SELECT * FROM biz_business WHERE industry = #{industry} AND deleted = 0")
    List<Business> findByIndustry(@Param("industry") String industry);
    
    /**
     * 根据企业规模查询企业列表
     * @param companySize 企业规模
     * @return 企业列表
     */
    @Select("SELECT * FROM biz_business WHERE company_size = #{companySize} AND deleted = 0")
    List<Business> findByCompanySize(@Param("companySize") String companySize);
    
    /**
     * 更新企业认证状态
     * @param businessId 企业ID
     * @param verificationStatus 认证状态
     * @param verificationRemark 认证备注
     * @return 更新结果
     */
    @Update("UPDATE biz_business SET verification_status = #{verificationStatus}, " +
            "verification_remark = #{verificationRemark}, verification_time = NOW(), " +
            "update_time = NOW() WHERE id = #{businessId}")
    int updateVerificationStatus(@Param("businessId") Long businessId, 
                               @Param("verificationStatus") String verificationStatus,
                               @Param("verificationRemark") String verificationRemark);
    
    /**
     * 更新企业账户状态
     * @param businessId 企业ID
     * @param accountStatus 账户状态
     * @return 更新结果
     */
    @Update("UPDATE biz_business SET account_status = #{accountStatus}, update_time = NOW() WHERE id = #{businessId}")
    int updateAccountStatus(@Param("businessId") Long businessId, @Param("accountStatus") String accountStatus);
    
    /**
     * 更新企业订阅计划
     * @param businessId 企业ID
     * @param subscriptionPlan 订阅计划
     * @param subscriptionStartTime 开始时间
     * @param subscriptionEndTime 结束时间
     * @return 更新结果
     */
    @Update("UPDATE biz_business SET subscription_plan = #{subscriptionPlan}, " +
            "subscription_start_time = #{subscriptionStartTime}, " +
            "subscription_end_time = #{subscriptionEndTime}, " +
            "update_time = NOW() WHERE id = #{businessId}")
    int updateSubscriptionPlan(@Param("businessId") Long businessId,
                             @Param("subscriptionPlan") String subscriptionPlan,
                             @Param("subscriptionStartTime") String subscriptionStartTime,
                             @Param("subscriptionEndTime") String subscriptionEndTime);
    
    /**
     * 更新企业最后活跃时间
     * @param businessId 企业ID
     * @return 更新结果
     */
    @Update("UPDATE biz_business SET last_active_time = NOW(), update_time = NOW() WHERE id = #{businessId}")
    int updateLastActiveTime(@Param("businessId") Long businessId);
    
    /**
     * 统计企业总数
     * @return 企业总数
     */
    @Select("SELECT COUNT(*) FROM biz_business WHERE deleted = 0")
    Long countTotalBusiness();
    
    /**
     * 根据认证状态统计企业数量
     * @param verificationStatus 认证状态
     * @return 企业数量
     */
    @Select("SELECT COUNT(*) FROM biz_business WHERE verification_status = #{verificationStatus} AND deleted = 0")
    Long countByVerificationStatus(@Param("verificationStatus") String verificationStatus);
    
    /**
     * 根据订阅计划统计企业数量
     * @param subscriptionPlan 订阅计划
     * @return 企业数量
     */
    @Select("SELECT COUNT(*) FROM biz_business WHERE subscription_plan = #{subscriptionPlan} AND deleted = 0")
    Long countBySubscriptionPlan(@Param("subscriptionPlan") String subscriptionPlan);
    
    /**
     * 查询即将到期的企业
     * @param days 天数
     * @return 即将到期的企业列表
     */
    @Select("SELECT * FROM biz_business WHERE subscription_end_time <= DATE_ADD(NOW(), INTERVAL #{days} DAY) " +
            "AND account_status = 'ACTIVE' AND deleted = 0")
    List<Business> findExpiringBusiness(@Param("days") Integer days);
    
    /**
     * 查询试用期企业
     * @return 试用期企业列表
     */
    @Select("SELECT * FROM biz_business WHERE subscription_plan = 'TRIAL' AND deleted = 0")
    List<Business> findTrialBusiness();
}